package com.ling.pl.security.controller;

import com.ling.pl.security.model.DefaultOrg;
import com.ling.pl.security.model.DefaultRole;
import com.ling.pl.security.model.DefaultUser;
import com.ling.pl.security.service.DefaultCompanyService;
import com.ling.pl.security.service.DefaultOrgService;
import com.ling.pl.security.service.DefaultRoleService;
import com.ling.pl.security.service.DefaultUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author bo.wang
 */
@RequestMapping("/defaultUser")
@Api(value = "defaultUser", description = "API")
@RestController("defaultUserController")
public class DefaultUserController extends BaseDefaultUserController {

    Log log = LogFactory.getLog(DefaultUserController.class);

    @Resource
    DefaultOrgService orgService;
    @Resource
    DefaultRoleService roleService;
    @Resource
    DefaultUserService userService;
    @Resource
    DefaultCompanyService companyService;

    @ApiOperation(value = "查询用户所在机构")
    @RequestMapping(value = "/loadUserOrgs", method = RequestMethod.GET)
    public Collection<DefaultOrg> loadUserOrgs(@ApiParam(value = "username") @RequestParam(name = "username") String username) {
        return orgService.loadUserOrgs(username);
    }

    @ApiOperation(value = "查询用户所在角色")
    @RequestMapping(value = "/loadUserRoles", method = RequestMethod.GET)
    public Collection<DefaultRole> loadUserRoles(@ApiParam(value = "username") @RequestParam(name = "username") String username) {
        return roleService.loadUserRoles(username);
    }

    /**
     * 这个方法用来判断在添加新用户时用户名是否已经存在
     *
     * @param username 用户输入的用户名
     */
    @ApiOperation(value = "查询用户名是否存在")
    @RequestMapping(value = "/userIsExists", method = RequestMethod.GET)
    public String userIsExists(@ApiParam(value = "username") @RequestParam(name = "username") String username) {
        String result = userService.userIsExists(username);
        return result;
    }

    /**
     * 重置指定用户密码
     *
     * @param username
     * @return
     */
    @ApiOperation(value = "重置用户密码")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.PUT)
    public String resetPassword(@ApiParam(value = "username", required = true) @RequestBody String username) {
        Integer newPassword = (int) (Math.random() * 900000 + 100000);
        userService.changePassword(username, newPassword.toString());
        return newPassword.toString();
    }

    @ApiOperation(value = "新增用户部门")
    @RequestMapping(value = "/insertUserPosition", method = RequestMethod.PUT)
    public void insertUserPosition(@ApiParam(value = "username", required = true) @RequestBody String username, @ApiParam(value = "ids", required = true) @RequestBody String ids) {
        userService.insertUserPosition(username, ids);
    }

    @ApiOperation(value = "新增用户机构")
    @RequestMapping(value = "/insertUserOrg", method = RequestMethod.PUT)
    public void insertUserOrg(@ApiParam(value = "username", required = true) @RequestBody String username, @ApiParam(value = "ids", required = true) @RequestBody String ids) {
        userService.insertUserOrg(username, ids);
    }

    @ApiOperation(value = "注册用户和用户")
    @RequestMapping(value = "/registerCompanyAndUser", method = RequestMethod.POST)
    public void registerCompanyAndUser(Map<String, Object> companyAndUser) throws Exception {
        Map<String, Object> company = (Map<String, Object>) companyAndUser.get("company");
        Map<String, Object> user = (Map<String, Object>) companyAndUser.get("user");
        this.companyService.registerCompany((String) company.get("id"), (String) company.get("name"), (String) company.get("desc"));
        this.userService.registerAdministrator((String) user.get("username"), (String) user.get("cname"), (String) user.get("ename"), (String) user.get("password"), (String) user.get("email"), (String) user.get("mobile"), (String) company.get("id"));
    }

    @ApiOperation(value = "查询用户名是否存在")
    @RequestMapping(value = "/checkUserExist", method = RequestMethod.GET)
    public String checkUserExist(String username) {
        String result = null;
        DefaultUser user = userService.loadUserByUsername(username);
        if (user != null) {
            result = "用户已存在！";
        }
        return result;
    }

    @ApiOperation(value = "变更密码")
    @RequestMapping(value = "/changePassword", method = RequestMethod.PUT)
    public void changePassword(String username, String newPassword) {
        userService.changePassword(username, newPassword);
    }

    @ApiOperation(value = "检查密码")
    @RequestMapping(value = "/checkPassword", method = RequestMethod.PUT)
    public String checkPassword(String username, String password) {
        return userService.checkPassword(username, password);
    }

    @ApiOperation(value = "查询当前在线用户数量")
    @RequestMapping(value = "/getLoginuserCount", method = RequestMethod.GET)
    public int getLoginuserCount() {
        return 1;
    }

    @ApiOperation(value = "查询当前在线用户列表")
    @RequestMapping(value = "/loadAllLoginUsers", method = RequestMethod.GET)
    public List<Map<String, Object>> loadAllLoginUsers() {
//        for (Object obj : users) {
//            DefaultUser user = (DefaultUser) obj;
//            Map<String, Object> map = new HashMap<String, Object>();
//            map.put("username", user.getUsername());
//            map.put("cname", user.getCname());
//            list.add(map);
//        }
        return new ArrayList<>();
    }
}

